package Sequence::Range;
use Sequence::Infinite::Interval;

sub new { shift;
  my $xs = shift;
  
  my $sequence = $xs->[0]; 
  if( not ( ref($sequence) and $sequence->isa('Sequence::Infinite') ) ){
    $sequence = new Sequence::Infinite::Natural;
  } else {
    $xs->[0] = $xs->[1];
    $xs->[1] = $xs->[2];
  }

  my $min = $xs->[0];
  my $next = $xs->[1];
  my $last = $xs->[ @{$xs} - 1 ];
  
  my $step = $next - $min;
  
  if( $last == Sequence::INF ) {
    new Sequence::Infinite::Interval( $sequence, $step, $min );
  } else {
    my $size = ( $last - $min + 1 ) / $step;
    my $range;
    
    for( my $i = 0; $i < $min; $i++ ) {
      $sequence->increment;
    }
      
    for( my $i = 0; $i < $size; $i++ ) {
      push @$range, $sequence->increment;
      $sequence->increment for 2..$step;
    }
      
    $range;
  }
}

1;
